package com.cskaoyan.service.wxservice.impl;

import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.po.MarketTopicExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.wxtopic.TopicWxVO;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.service.wxservice.TopicWxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author:J
 * @datetime 2023/1/1 19:40
 * @description: TODO:
 */

@Service
public class TopicWxServiceImpl implements TopicWxService {

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<MarketTopic> list(BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketTopicExample example = new MarketTopicExample();

        //排序
        //example.setOrderByClause(info.getSort()+" "+info.getOrder());
        //MarketTopicExample.Criteria criteria=example.createCriteria();

        List<MarketTopic> list = marketTopicMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Override
    public TopicWxVO detail(Integer id) {
        //data由两张表组成，先去topic表中查询goods编号表 ,遍历goods，取出goods[]中id，
        // 再根据id查出goods表信息

        TopicWxVO topicWxVO = new TopicWxVO();

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        List<MarketTopic> lists = marketTopicMapper.selectByExample(marketTopicExample);

        List<MarketGoods> marketGoodsLists = new ArrayList<>();

        for (MarketTopic marketTopic : lists) {
            //遍历topic表，得到goods[]的id表
            Integer[] goodsNum = marketTopic.getGoods();
//            try {
                for (Integer good : goodsNum) {
                    //遍历goods[]id表，将根据id查询goods表得到的信息加入goodsLists中
                    marketGoodsLists.add(marketGoodsMapper.selectByPrimaryKey(good));
                }
//            } catch (Exception e) {
//                marketGoodsLists = null;
//            }
        }
        topicWxVO.setMarketTopicList(lists);
        topicWxVO.setMarketGoodsList(marketGoodsLists);

        return topicWxVO;
    }

    @Override
    public CommonData<MarketTopic> related(Integer id) {
        //TODO
        //MarketTopic中Goods表得到id,

        List<MarketTopic> list =new ArrayList<>();
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        //MarketTopicExample.Criteria criteria= marketTopicExample.createCriteria();
        Integer[] goodsNums = marketTopicMapper.selectByPrimaryKey(id).getGoods();
        for (Integer goodsNum : goodsNums) {
            try {
                list.add(marketTopicMapper.selectByPrimaryKey(goodsNum));
            } catch (Exception e) {
                list = null;
            }
        }

        //固定id
        Integer[] goodsNums1 = {264, 268, 272, 274};
        for (Integer goodsNum : goodsNums1) {
            list.add(marketTopicMapper.selectByPrimaryKey(goodsNum));
        }

        PageInfo pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }
}
